#include "EscapeRune.h"

EscapeRune::EscapeRune()
{
}

void EscapeRune::loadResource()
{
	endT = GameEnd;
	GameTime = EndTime;
	SplashTime = 10;
	ts = new TileSystem(30,30);
	ts->readTiles("MapDesign.csv");
	ts->setSprite("images/Escape/tiles.tga", 8, 8);
	_Char = new Object2D(30,30);
	_Char->setPosition(31,240);	
	_Char2 = new Object2D(30,30);
	_Char2->setPosition(31,540);
	_Jumping = false;
	_Point = 0;
	_Point2 = 0;
	point1 = 0;
	point2 = 0;
	_Sky = new Object2D(800,600);
	_Sky->setPosition(400,300);
	_Tree = new Object2D(1200,100);
	_Tree->setPosition(400,630);
	SignalPage = Object2D(800.0, 600.0, 0.0, false);
	SignalPage.setPosition(400.0, 300.0);
	LoadTGA(&_TreeTexture, "images/Escape/tree.tga");
	LoadTGA(&_SkyTexture, "images/Escape/sky.tga");
	_CharImage = new TextureImage[5];
	_CharImage2 = new TextureImage[5];
	_CharIndex = 0;
	_CharIndex2 = 0;
	P1face = 0;
	P2face = 0;

	Time_Word = Object2D(60.0, 30.0, 0.0, false);
	Time_Tenth = Object2D(45.0, 45.0, 0.0, false);
	Time_One = Object2D(45.0, 45.0, 0.0, false);
	P1Score_Word = Object2D(60.0, 30.0, 0.0, false);
	P1Score_Tenth = Object2D(45.0, 45.0, 0.0, false);
	P1Score_One = Object2D(45.0, 45.0, 0.0, false);
	P2Score_Word = Object2D(60.0, 30.0, 0.0, false);
	P2Score_Tenth = Object2D(45.0, 45.0, 0.0, false);
	P2Score_One = Object2D(45.0, 45.0, 0.0, false);

	Time_Word.setPosition(430.0, 20.0);
	P1Score_Word.setPosition(50,25);
	P2Score_Word.setPosition(50,325);

	LoadTGA(&_CharImage[0], "images/Escape/1.tga");
	LoadTGA(&_CharImage[1], "images/Escape/2.tga");
	LoadTGA(&_CharImage[2], "images/Escape/3.tga");
	LoadTGA(&_CharImage[3], "images/Escape/4.tga");
	LoadTGA(&_CharImage[4], "images/Escape/5.tga");

	LoadTGA(&_CharImage2[0], "images/Escape/1.tga");
	LoadTGA(&_CharImage2[1], "images/Escape/2.tga");
	LoadTGA(&_CharImage2[2], "images/Escape/3.tga");
	LoadTGA(&_CharImage2[3], "images/Escape/4.tga");
	LoadTGA(&_CharImage2[4], "images/Escape/5.tga");

	LoadTGA( &Image_SplashTime[0], "images/Escape/ThrowGarbage_Intro_1.tga", false);
	LoadTGA( &Image_SplashTime[1], "images/Escape/ThrowGarbage_Intro_1.tga", false);
	LoadTGA( &Image_SplashTime[2], "images/Escape/ThrowGarbage_Tutorial.tga", false);	
	LoadTGA( &Image_SplashTime[3], "images/Escape/Image_Ready.tga", false);
	LoadTGA( &Image_SplashTime[4], "images/Escape/Image_Go.tga", false);

	LoadTGA( &GameOver, "images/Escape/sky.tga", false);
	P1run[0] = new Sprite(4,1, "images/Escape/Garbagerun.tga");
	P1run[1] = new Sprite(4,1, "images/Escape/Garbagerun.tga");
	P2run[0] = new Sprite(4,1, "images/Escape/Garbagerun2.tga");
	P2run[1] = new Sprite(4,1, "images/Escape/Garbagerun.tga");

	LoadTGA( &Hud[0], "images/Escape/Image_Time.tga", false);
	LoadTGA( &Hud[1], "images/Escape/Image_P1Score.tga", false);
	LoadTGA( &Hud[2], "images/Escape/Image_P2Score.tga", false);
}

void EscapeRune::freeResource()
{
}

void EscapeRune::reset()
{
}

void EscapeRune::AIMove(int key, int x, int y)
{

	if(key == 'j' || key == 'J')
	{
        PlaySound("sounds/games/button.wav", NULL,SND_ASYNC);		
		_Char2->move(MOVE_SPEED/5, 0);
		_CharIndex2++;
		if(_CharIndex2 > 5)
			_CharIndex2 = 0;
		if(ts->collideRight(*_Char2,_CollideTile))
			_Char2->move(-MOVE_SPEED, 0);
		if(_CollideTile.getType() == 57)
		{
			++_Point2;
			_Char2->setPosition(31,540);	
			PlaySound("sounds/escape/DingLing.wav",NULL,SND_FILENAME|SND_ASYNC);
		}
	}

	if(key == 'l' || key == 'L')
	{
        PlaySound("sounds/games/button.wav", NULL,SND_ASYNC);		
		_Char2->move(MOVE_SPEED/5, 0);
		_CharIndex2++;
		if(_CharIndex2 > 5)
			_CharIndex2 = 0;
		if(ts->collideRight(*_Char2,_CollideTile))
			_Char2->move(-MOVE_SPEED, 0);
		if(_CollideTile.getType() == 57)
		{
			++_Point2;
			_Char2->setPosition(31,540);	
			PlaySound("sounds/escape/DingLing.wav",NULL,SND_FILENAME|SND_ASYNC);
		}
	}
}
void EscapeRune::draw()
{
		

	if(SplashTime > 9)
		SignalPage.drawPlane(Image_SplashTime[0].texID);
	else if(SplashTime > 7)
		SignalPage.drawPlane(Image_SplashTime[1].texID);
	else if(SplashTime > 3)
		SignalPage.drawPlane(Image_SplashTime[2].texID);
	else if(SplashTime > 2)
	{
		SignalPage.drawPlane(Image_SplashTime[3].texID);
	}
	else if(SplashTime > 1)
	{
		SignalPage.drawPlane(Image_SplashTime[4].texID);
	}

	if(SplashTime == 0 && GameTime != 0)
	{
		
	_Sky->drawPlane(_SkyTexture.texID);
	_Tree->drawPlane(_TreeTexture.texID);
	ts->draw();
	
	_Char->drawPlane(P1run[P1face], _CharIndex);
	_Char2->drawPlane(P2run[P1face], _CharIndex2);
	
	printw(15,35,0, "            : %d", _Point);
	printw(15,335,0, "            : %d", _Point2);
	printw(300.0, 50.0, 0.0,"                   %3.1f", GameTime);

	// Time(HUD)
	Time_Word.drawPlane(Hud[0].texID);

	// Players' score(HUD)
	P1Score_Word.drawPlane(Hud[1].texID);
	P2Score_Word.drawPlane(Hud[2].texID);

	}

	if(GameTime == 0)
	{


		SignalPage.drawPlane(GameOver.texID);	
		printw(200.0, 200.0, 0.0,"Player 1's Total Score: %3.1f", point1);
		printw(200.0, 250.0, 0.0,"Player 2's Total Score: %3.1f", point2);
	}
	
}

void EscapeRune::update()
{
	if(SplashTime != 0)
	{
		float TimePassed = 1 / RealTimeController::getInstance().getFPS();
		SplashTime -= TimePassed;

		if(SplashTime < 0)
			SplashTime = 0;
	}

	if(SplashTime == 0)
	{
		float TimePassed = 1 / RealTimeController::getInstance().getFPS();
		GameTime -= TimePassed;

		if(GameTime < 0)
			GameTime = 0;
	}
		
	
	if(GameTime != 0 && SplashTime == 0)
	{
	_Char->move(_UpVel);
	_Char2->move(_UpVel);

	
	if(_UpVel.getY() < 0)
		_UpVel += Vector2D<float>(0, JUMP_FORCE/10);
	else
		_Jumping = false;
	if(ts->collideTop(*_Char, _CollideTile))
	{
		switch(_CollideTile.getType())
		{
		case 6:
			ts->setTile(_CollideTile.getRow(), _CollideTile.getCol(), 14);
			break;
		case 14:
			ts->setTile(_CollideTile.getRow(), _CollideTile.getCol(), 0);
			break;
		case 57:
			_Point++;
			ts->setTile(_CollideTile.getRow(), _CollideTile.getCol(), 0);
			PlaySound("sounds/escape/DingLing.wav",NULL,SND_FILENAME|SND_ASYNC);
			break;
		}
	
		_UpVel = Vector2D<float>();
		_Char->move(_GRAVITY2D_/2);
		_Char2->move(_GRAVITY2D_/2);
		
	}
		_Char->move(_GRAVITY2D_/2);
		if(ts->collideBelow(*_Char2, _CollideTile) != 0)
		{
			_Char->move(_GRAVITY2D_/2*-1);
		}
		_Char2->move(_GRAVITY2D_/2);
		if(ts->collideBelow(*_Char2, _CollideTile) != 0)
		{
			_Char2->move(_GRAVITY2D_/2*-1);
		}
		if(_SinglePlayer == true)
		{

			if(GameTime != 0)
			{
				AIMove('l',0,0);
			}
			if(_Char->getPosition().getX() < _Char2->getPosition().getX())
			{
				AIMove('l',0,0);
			}
			if(_Point2 > _Point)
			{
				_Char2->move(MOVE_SPEED/13, 0);
			}

			
		}
	}

	if(GameTime == 0)
	{
		point1 = _Point;
		point2 = _Point2;
		// Update game end time
	    float TimePassed = 1 / RealTimeController::getInstance().getFPS();
	    endT -= TimePassed;
	}

	if(endT < 0)
	{
		(point1 > point2) ? LeaderBoard::getInstance().P1_AddScore() : LeaderBoard::getInstance().P2_AddScore();
		SceneManager::getInstance().changeScene("Score_Board");
	}
	
}

void EscapeRune::handleKeyboard(int key, int x, int y)
{
	switch(key)
	{
	case 116:
		SceneManager::getInstance().setOverlayScene("Pause");
		break;
	case 97:
		PlaySound("sounds/games/button.wav", NULL,SND_ASYNC);		
		_Char->move(MOVE_SPEED, 0);
		_CharIndex++;
		if(_CharIndex > 5)
			_CharIndex = 0;
		if(ts->collideRight(*_Char,_CollideTile))
			_Char->move(-MOVE_SPEED, 0);
		if(_CollideTile.getType() == 57)
		{
			++_Point;
			_Char->setPosition(31,240);	
			PlaySound("sounds/escape/DingLing.wav",NULL,SND_FILENAME|SND_ASYNC);
		}
		break;

	case 100:
		PlaySound("sounds/games/button.wav", NULL,SND_ASYNC);	
		_Char->move(MOVE_SPEED, 0);
		_CharIndex++;
		if(_CharIndex > 5)
			_CharIndex = 0;
		if(ts->collideRight(*_Char,_CollideTile))
			_Char->move(-MOVE_SPEED, 0);
		if(_CollideTile.getType() == 57)
		{
			++_Point;
			_Char->setPosition(31,240);	
			PlaySound("sounds/escape/DingLing.wav",NULL,SND_FILENAME|SND_ASYNC);
		}
		break;
	if(_SinglePlayer == false)
	{
	case 106:
		PlaySound("sounds/games/button.wav", NULL,SND_ASYNC);		
		_Char2->move(MOVE_SPEED, 0);
		_CharIndex2++;
		if(_CharIndex2 > 5)
			_CharIndex2 = 0;
		if(ts->collideRight(*_Char2,_CollideTile))
			_Char2->move(-MOVE_SPEED, 0);
		if(_CollideTile.getType() == 57)
		{
			++_Point2;
			_Char2->setPosition(31,540);	
			PlaySound("sounds/escape/DingLing.wav",NULL,SND_FILENAME|SND_ASYNC);
		}
		break;

	case 108:
		PlaySound("sounds/games/button.wav", NULL,SND_ASYNC);		
		_Char2->move(MOVE_SPEED, 0);
		_CharIndex2++;
		if(_CharIndex2 > 5)
			_CharIndex2 = 0;
		if(ts->collideRight(*_Char2,_CollideTile))
			_Char2->move(-MOVE_SPEED, 0);
		if(_CollideTile.getType() == 57)
		{
			++_Point2;
			/*ts->setTile(_CollideTile.getRow(), _CollideTile.getCol(), 0);*/
			_Char2->setPosition(31,540);	
			PlaySound("sounds/escape/DingLing.wav",NULL,SND_FILENAME|SND_ASYNC);
		}
		break;
	}
	}
}
void EscapeRune::handleKeyboardUp(int key, int x, int y)
{
	if(key == ' ' && SplashTime != 0)
	{
		PlaySound("sounds/games/go.wav", NULL,SND_ASYNC);
		SplashTime = 3;
	}
}
void EscapeRune::handleKeyboardSpecial(int key, int x, int y)
{
	switch ( key ) {
	case GLUT_KEY_UP : 
		//if(_Jumping == false)
		//{
		//	_UpVel = Vector2D<float>(0, -JUMP_FORCE);
		//	PlaySound("bounce.wav",NULL,SND_FILENAME|SND_ASYNC);
		//	
		//	_Jumping = true;
		//}
		break ;
	case GLUT_KEY_DOWN : break ;
	case GLUT_KEY_LEFT :
		break ;

	case GLUT_KEY_RIGHT :
		break ;
	case GLUT_KEY_F1 : break ;
	case GLUT_KEY_F2 : 
		break ;
	default: break;
	}
}

void EscapeRune::getMid(int &x, int &y)
{
	x  = (800 + _Char->getPosition().getX()) /2 -50 ;
	y =	(600 + _Char->getPosition().getY()) /2 - 300;
}

void EscapeRune::handleMouseClick(int button, int state, int x, int y)
{
	switch( button )
	{
	case GLUT_LEFT_BUTTON :
		break;
	case GLUT_RIGHT_BUTTON : 
		break;
	case GLUT_MIDDLE_BUTTON : break;
	}
}
void EscapeRune::handleMouseMove(int x, int y)
{
}